package com.zbkj.front.controller.mszs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.utils.CrmebUtil;
import com.zbkj.service.thirdparty.pay.weixin.PayService;
import com.zbkj.service.thirdparty.pay.weixin.PayYwTypeHandleService;
import com.zbkj.service.thirdparty.pay.weixin.WeixinPayXcxService;
import com.zbkj.service.thirdparty.pay.weixin.enums.PayYwTypeEnum;
import com.zbkj.service.thirdparty.pay.weixin.request.PayRequest;
import com.zbkj.service.thirdparty.pay.weixin.request.WeixinPayXcxPayRequest;
import com.zbkj.service.thirdparty.pay.weixin.request.WeixinPayXcxV3Request;
import com.zbkj.service.thirdparty.pay.weixin.response.PayResponse;
import com.zbkj.service.thirdparty.pay.weixin.response.PayXcxResponse;
import com.zbkj.service.thirdparty.pay.weixin.vo.PayYwTypeRechargeIntegralVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 民宿助手-支付相关-controller层
 * @author lijie
 */
@Slf4j
@RestController
@RequestMapping(value = "api/front/mszs/pay")
@Api(tags = "民宿助手-支付相关")
public class MszsPayController {

    @Autowired
    private PayService payService;

    @Autowired
    private WeixinPayXcxService weixinPayXcxService;

    @Autowired
    private PayYwTypeHandleService payYwTypeService;

    @ApiOperation(value = "支付业务类型-充值积分")
    @RequestMapping(value = "/payYwType1001", method = RequestMethod.POST)
    public PayYwTypeRechargeIntegralVo payYwType1001() {
        return new PayYwTypeRechargeIntegralVo();
    }

    @ApiOperation(value = "支付(统一支付)")
    @RequestMapping(value = "/payAll", method = RequestMethod.POST)
    public CommonResult<PayResponse> payAll(@RequestBody @Validated PayRequest payRequest, HttpServletRequest request) {
        String ip = CrmebUtil.getClientIp(request);
        return CommonResult.success(payService.pay(payRequest, ip));
    }

    @ApiOperation(value = "微信支付(统一下单)")
    @RequestMapping(value = "/weixinPay", method = RequestMethod.POST)
    public CommonResult<PayResponse> weixinPay(@RequestBody @Validated PayRequest payRequest, HttpServletRequest request) {
        String ip = CrmebUtil.getClientIp(request);
        payRequest.setIp(ip);
        return CommonResult.success(payService.weixinPay(payRequest));
    }

    @ApiOperation(value = "微信支付-微信小程序支付(统一下单)")
    @RequestMapping(value = "/weixinXcxPay", method = RequestMethod.POST)
    public CommonResult<PayResponse> weixinXcxPay(@RequestBody @Validated WeixinPayXcxPayRequest payXcxPayRequest, HttpServletRequest request) {
        String ip = CrmebUtil.getClientIp(request);
        payXcxPayRequest.setIp(ip);
        return CommonResult.success(payService.weixinXcxPay(payXcxPayRequest));
    }

    @ApiOperation(value = "微信支付-微信小程序支付（V3）- 下单参数")
    @RequestMapping(value = "/weixinXcxPayV3", method = RequestMethod.POST)
    public CommonResult<PayXcxResponse> weixinXcxPayV3(@RequestBody @Validated WeixinPayXcxV3Request payParam) {
        //得到下单参数
        PayXcxResponse payXcxResponse =  weixinPayXcxService.weixinXcxPayV3(payParam);

        //验证支付业务-测试
        if(payParam.getYwType() == null || PayYwTypeEnum.TEST.equals(payParam.getYwType())){
            return CommonResult.success(payXcxResponse);
        }

        //验证支付业务-充值积分
        if(PayYwTypeEnum.Recharge_integral.equals(payParam.getYwType())){
            payYwTypeService.ywRechargeIntegral(payParam.getYwParams(), payXcxResponse.getOrderNo());
            return CommonResult.success(payXcxResponse);
        }
        return CommonResult.failed("支付业务类型错误！");
    }

    public static void main(String[] args) {
        PayYwTypeRechargeIntegralVo ywParamVo = new PayYwTypeRechargeIntegralVo();
        ywParamVo.setIntegral(10);
        ywParamVo.setIsLifelong(true);
        System.out.println(PayYwTypeEnum.Recharge_integral);
        PayYwTypeEnum payYwTypeEnum= PayYwTypeEnum.Recharge_integral;
        System.out.println(payYwTypeEnum);
        System.out.println(payYwTypeEnum.name());

        String ywParam = JSON.toJSONString(ywParamVo);
        System.out.println(ywParam);
        ywParamVo = JSONObject.parseObject(ywParam,PayYwTypeRechargeIntegralVo.class);
        System.out.println(ywParamVo);
    }

    @ApiOperation(value = "微信支付-微信小程序支付（V3）- 回调接口")
    @RequestMapping(value = "/wxXcxPayCallback", method = RequestMethod.POST)
    public Map<String,String> weixinXcxPayV3Callback(HttpServletRequest request) {
        return weixinPayXcxService.callback(request);
    }

    @ApiOperation(value = "微信支付-微信小程序支付（V3）- 查询支付结果")
    @RequestMapping(value = "/selectPayResult", method = RequestMethod.GET)
    @ApiImplicitParam(name = "outTradeNo",value = "订单号",required = true)
    public CommonResult<Boolean> selectPayResult(@RequestParam String outTradeNo) {
        return CommonResult.success(weixinPayXcxService.selectPayResult(outTradeNo));
    }

}
